package com.ifar.audiosdk.activity

import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.ifar.audiosdk.R
import com.ifar.audiosdk.base.BaseActivity
import com.ifar.audiosdk.common.IfarEventBusTag
import com.ifar.audiosdk.common.entity.UserInfoEntity
import com.ifar.audiosdk.model.call.viewmodel.CallInViewModel
import kotlinx.android.synthetic.main.activity_call_in.*
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber
import org.simple.eventbus.ThreadMode

@Route(path = "/app/CallInActivity")
class CallInActivity : BaseActivity<CallInViewModel>() {
    @Autowired
    lateinit var avatar: String

    @Autowired
    lateinit var permission: String

    @Autowired
    lateinit var name: String

    @Autowired
    lateinit var id: String

    @Autowired
    lateinit var channel: String

    override fun getLayoutResId(): Int = R.layout.activity_call_in

    override fun initView() {
        civAvater.setImageDrawable(avatarDrawable())
        Glide.with(this).load(avatar).error(avatarDrawable()).into(civAvater)

        tvCallName.text = "$name 来电"

        ivHangup.setOnClickListener {
            EventBus.getDefault().post(
                UserInfoEntity(id, name, avatar, permission),
                IfarEventBusTag.CALL_IN_HANGUP
            )
            finish()
        }

        ivAnswer.setOnClickListener {
            EventBus.getDefault().post(
                UserInfoEntity(id, name, avatar, permission),
                IfarEventBusTag.CALL_IN_ACCEPT
            )
            finish()
        }
    }

    private fun avatarDrawable() = if (permission == "3") {
        ContextCompat.getDrawable(mContext, R.drawable.ifar_online_wx)
    } else {
        ContextCompat.getDrawable(mContext, R.drawable.ifar_online_zj)
    }

    override fun initData() {
    }

    @Subscriber(tag = IfarEventBusTag.CALL_IN_OTHER_HANGUP, mode = ThreadMode.MAIN)
    fun callInHangup(string: String) {
        finish()
    }
}